﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        private string strCode;

        //窗口初始化事件
        private void Login_Load(object sender, EventArgs e)
        {
            createCheckCodeImage(generateCheckCode());
        }

        //登录按钮事件
        private void go_Click(object sender, EventArgs e)
        {

            //1:输入检测
            if (userName.Text.Trim() == String.Empty || password.Text.Trim() == String.Empty)
            {
                MessageBox.Show("账号或用户名不能为空，请重新输入");
                return;
            }

            //2:验证码检测
            if (code.Text != strCode)
            {
                MessageBox.Show("验证码错误，请重新输入");
                return;
            }
            
            //3:用户登录检测
            if (role_Check())
            {
                //Uid标签，用来获取用户ID的值
                uid.Text = BLL.User.getUserIdByName(userName.Text);
                MessageBox.Show("Welcome back!" + (uid.Text));

                this.Enabled = false;
                this.Hide();
                Main m = new Main();
                m.Show(this);
            }
            else {

                MessageBox.Show("请重新检查账号或者密码！");
                createCheckCodeImage(generateCheckCode());
            
            }

        }

        //注册按钮事件
        private void register_Click(object sender, EventArgs e)
        {
            new User.UserRegister().Show();
        }

        //验证码单击事件
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            createCheckCodeImage(generateCheckCode());
        }

        //《==========身份验证方法============》

        //身份验证
        private bool role_Check() {


            //用户角色判断
            if (BLL.User.isUser(userName.Text, password.Text))
            {

                role.Text = "1";

                return true;

            }

            //管理员角色判断
            else if (BLL.Admin.isAdmin(userName.Text, password.Text))
            {
                role.Text = "0";

                return true;
            
            }

            else{
            
                return false;
                
            }


            
            
        }

        //《==========验证码方法============》

        //验证码部分1：随机生成4位数的数字
        private string generateCheckCode() {

            int number;
            char code;
            string checkCode = String.Empty;
            Random random = new Random();
            for (int i = 0; i < 4; i++ )
            {
                number = random.Next();
                if (number % 2 == 0)
                {

                    code = (char)('0' + (char)(number % 10));

                }
                else {

                    code = (char)('A' + (char)(number % 26));

                }
                checkCode += code.ToString();

            }

            strCode = checkCode;
            return checkCode;
        }

        //验证码部分2：
        private void createCheckCodeImage(String checkCode) { 
        
            if(checkCode == null || checkCode.Trim() == String.Empty){

                return;
            
            }


            System.Drawing.Bitmap image = 
                new System.Drawing.Bitmap((int)Math.Ceiling(checkCode.Length * 12.5) + 18, 30);
            Graphics g = Graphics.FromImage(image);

            try {

                Random random = new Random();

                g.Clear(Color.White);

                for(int i = 0; i<2; i++){
                
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Pink), x1, y1, x2, y2);
                }

                Font font = new Font("微软雅黑", 12, FontStyle.Bold);
                System.Drawing.Drawing2D.LinearGradientBrush brush = 
                    new System.Drawing.Drawing2D.LinearGradientBrush(
                        new Rectangle(0,0,image.Width, image.Height),Color.Purple, Color.DarkCyan,1.2f,true);
                
                //画出验证码
                g.DrawString(checkCode, font, brush, 2, 2);

                for (int i = 0; i < 150;i++ )
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x,y,Color.FromArgb(random.Next()));
                }

                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                this.pictureBox1.Width = image.Width;
                this.pictureBox1.Height = image.Height;
                this.pictureBox1.BackgroundImage = image;


            }catch{
            
            
            
            }


        
        }








    }
}
